<?php
// +----------------------------------------------------------------------
// | LIKE [ JUST DO IT ]
// +----------------------------------------------------------------------
// | Author: Mr.hu <huhaiyang7788@163.com>
// +----------------------------------------------------------------------
// | DESC: 后台登录
// +----------------------------------------------------------------------

namespace api\service\login;


use api\models\LoginModel;
use api\service\Login;
use common\controllers\Functions;
use common\interfaces\Token;

class Admin implements Login
{

    public $tokenObj;           //token实例

    /**
     * 登录并返回token
     * @param $data
     * return token
     * @ Mr.hu
     */
    public function logIn($data)
    {
        $user_name=$data['user_name'];
        $pass_word=$data['pass_word'];
        $info=LoginModel::find()->where(['user_name'=>$user_name])->select('pass_word')->one();
        //密码验证通过
        if(Functions::decodePass($pass_word)===$info['pass_word']) {
            //生成对应的token，存储并返回
            return $this->tokenObj->createToken();
        }else{
            return false;
        }
    }


    /**
     * 登出
     * @param $token
     * return void
     * @ Mr.hu
     */
    public function logOut($token)
    {
        return $this->tokenObj->delToken($token);
    }


    /**
     * 设置token实例对象
     * @param Token $token
     * return void
     * @ Mr.hu
     */
    public function setTokenService(Token $token)
    {
        $this->tokenObj=$token;
    }

}